package com.hwl.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwl.eduservice.mapper.CourseCollectMapper;
import com.hwl.eduservice.pojo.CourseCollect;
import com.hwl.eduservice.pojo.vo.CourseCollectVo;
import com.hwl.eduservice.service.CourseCollectService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author long
 * @since 2021-09-21
 */
@Service
public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectMapper, CourseCollect> implements CourseCollectService {

    @Override
    public Boolean isCollect(String courseId, String memberId) {
        QueryWrapper<CourseCollect> queryWrapper = new QueryWrapper<CourseCollect>()
                .eq("course_id", courseId).eq("member_id", memberId);
        return baseMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public boolean saveCourseCollect(String courseId, String memberId) {
        //未收藏则收藏
        if (!this.isCollect(courseId, memberId)) {
            CourseCollect courseCollect = new CourseCollect().setCourseId(courseId).setMemberId(memberId);
            return this.save(courseCollect);
        }
        return true;
    }

    @Override
    public List<CourseCollectVo> selectListByMemberId(String memberId) {
        return baseMapper.selectPageByMemberId(memberId);
    }

    @Override
    public boolean removeCourseCollect(String courseId, String memberId) {
        //已收藏则删除
        if (this.isCollect(courseId, memberId)) {
            QueryWrapper<CourseCollect> queryWrapper = new QueryWrapper<CourseCollect>()
                    .eq("course_id", courseId).eq("member_id", memberId);
            return this.remove(queryWrapper);
        }
        return true;
    }
}
